<?php

namespace App\Common\Web;


use App\Common\Base;
use App\Common\Log;
use App\Common\Sys;
use App\Model\t_wx_reply;
use App\X\EHelper;
use XWX\Common\H;
use XWX\Common\XUrl;

class TempView extends Base
{
    protected $pub_view_path;
    protected $pub_replace_data = [];


    public function __construct($view_path)
    {
        $this->pub_view_path = EHelper::funcGetConf('SYS.view') . $view_path;
    }


    public function funcPageContentBefore()
    {

    }


    final public function funcPageContent()
    {
        $this->funcPageContentBefore();

        $page_content = file_get_contents($this->pub_view_path);
        if ($page_content === false)
        {
            return $this->funcGetR(404, 'FILE NOT FIND');
        }


        $this->funcReplace($page_content);


        $r = $this->funcGetR(0, '');
        $r->setData('page_content', $page_content);


        return $r;
    }


    public function funcSetReplace(array $data)
    {
        $this->pub_replace_data = $data;
    }


    /**
     * 替换占位符
     */
    private function funcReplace(& $content)
    {
        $replace_data = [];


        $view_path = EHelper::funcGetConf('SYS.view');

        //套入其他模板内容
        $p = '#\{include file=\'(.*?)\'}#i';
        preg_match_all($p, $content, $match);
        $o_file_rooms = $match[0];
        $o_file_paths = $match[1];
        foreach ($o_file_paths as $k => $v)
        {
            $xurl = new XUrl($v);

            $file_name = $xurl->url();//_template/head/head.wx.jssdk.html?dev=tru
            $file_path = "{$view_path}{$file_name}";

            $key = $o_file_rooms[$k];

            $o_content = file_get_contents($file_path);
            if ($o_content !== false)
            {
                $class_name__tf_other_fix = str_replace('.', '_', $xurl->page_name());
                $class_name__tf_other = sprintf('App\Common\Web\TempFileOther\%s', $class_name__tf_other_fix);
                Log::funcLogDebug(__METHOD__, $class_name__tf_other_fix);
                if (class_exists($class_name__tf_other))
                {
                    $app_tf_other = new $class_name__tf_other($xurl);
                    $app_tf_other->funcHandle($o_content);
                }

                $replace_data[$key] = $o_content;
            }
            else
            {
                $replace_data[$key] = "<!-- {$v} NOT FIND -->";
            }
        }


        //静态资源域名
        $replace_data['{s_file_root}'] = Sys::funcStaticFileRoot();
//        $replace_data['{s_file_app}'] = Sys::funcStaticFileRoot() . "_app/{$app_cookie->getAppId()}/{$this->funcGetTF()}";
        $replace_data['{x_res}'] = EHelper::funcGetConf('SYS.static_file_root') . "_app/_x_res";
        $replace_data['{x_res_common}'] = EHelper::funcGetConf('SYS.static_file_root') . "_app/_x_res_common";
        $replace_data['{v}'] = (int)(H::now()->timestamp / 600); //10分钟变化一次


        $replace_data = array_merge($replace_data, $this->pub_replace_data);

        //进行替换
        foreach ($replace_data as $k => $v)
        {
            $content = str_replace($k, $v, $content);
        }
    }
}